home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Filling Shapes with a Gradient Brush / Creating a Path Gradient / Specifying Points on the Boundary / APP1 / GDITEST81.dpr
Encoding:
Text File  |  2003-10-15  |  4.1 KB  |  139 lines

  1. program GDITEST81;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10. // The following example constructs a path gradient brush from a star-shaped
  11. // path. The code calls the SetCenterColor method to set the color at the
  12. // centroid of the star to red. Then the code calls the SetSurroundColors method
  13. // to specify various colors (stored in the colors array) at the individual
  14. // points in the points array. The final code statement fills the star-shaped
  15. // path with the path gradient brush.
  16.  
  17. Procedure OnPaint(DC: HDC);
  18. var
  19.   graphics : TGPGraphics;
  20.   path: TGPGraphicsPath;
  21.   pthGrBrush: TGPPathGradientBrush;
  22.   colors: array[0..9] of TGPColor;
  23.   count: Integer;
  24. const
  25.   // Put the points of a polygon in an array.
  26.   points : array[0..9] of TGPPoint =
  27.     ((x: 75 ; y: 0  ), (x: 100; y: 50 ),
  28.      (x: 150; y: 50 ), (x: 112; y: 75 ),
  29.      (x: 150; y: 150), (x: 75 ; y: 100),
  30.      (x: 0  ; y: 150), (x: 37 ; y: 75 ),
  31.      (x: 0  ; y: 50 ), (x: 50 ; y: 50 ));
  32. begin
  33.   graphics := TGPGraphics.Create(DC);
  34.  
  35.   // Use the array of points to construct a path.
  36.   path:= TGPGraphicsPath.Create;
  37.   path.AddLines(PGPPoint(@points), 10);
  38.  
  39.   // Use the path to construct a path gradient brush.
  40.   pthGrBrush:= TGPPathGradientBrush.Create(path);
  41.  
  42.   // Set the color at the center of the path to red.
  43.   pthGrBrush.SetCenterColor(MakeColor(255, 255, 0, 0));
  44.  
  45.   // Set the colors of the points in the array.
  46.   colors[0] := MakeColor(255, 0, 0, 0);
  47.   colors[1] := MakeColor(255, 0, 255, 0);
  48.   colors[2] := MakeColor(255, 0, 0, 255);
  49.   colors[3] := MakeColor(255, 255, 255, 255);
  50.   colors[4] := MakeColor(255, 0, 0, 0);
  51.   colors[5] := MakeColor(255, 0, 255, 0);
  52.   colors[6] := MakeColor(255, 0, 0, 255);
  53.   colors[7] := MakeColor(255, 255, 255, 255);
  54.   colors[8] := MakeColor(255, 0, 0, 0);
  55.   colors[9] := MakeColor(255, 0, 255, 0);
  56.  
  57.   count := 10;
  58.   pthGrBrush.SetSurroundColors(@colors, count);
  59.  
  60.   // Fill the path with the path gradient brush.
  61.   graphics.FillPath(pthGrBrush, path);
  62.  
  63.   // Gamma correction
  64.   pthGrBrush.SetGammaCorrection(TRUE);
  65.   graphics.TranslateTransform(200.0, 0.0);
  66.   graphics.FillPath(pthGrBrush, path);
  67.  
  68.   path.Free;
  69.   pthGrBrush.Free;
  70.   graphics.Free;
  71. end;
  72.  
  73.  
  74. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  75. var
  76.   Handle: HDC;
  77.   ps: PAINTSTRUCT;
  78. begin
  79.   case message of
  80.     WM_PAINT:
  81.       begin
  82.         Handle := BeginPaint(Wnd, ps);
  83.         OnPaint(Handle);
  84.         EndPaint(Wnd, ps);
  85.         result := 0;
  86.       end;
  87.  
  88.     WM_DESTROY:
  89.       begin
  90.         PostQuitMessage(0);
  91.         result := 0;
  92.       end;
  93.  
  94.    else
  95.       result := DefWindowProc(Wnd, message, wParam, lParam);
  96.    end;
  97. end;
  98.  
  99. var
  100.   hWnd     : THandle;
  101.   Msg      : TMsg;
  102.   wndClass : TWndClass;
  103. begin
  104.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  105.    wndClass.lpfnWndProc    := @WndProc;
  106.    wndClass.cbClsExtra     := 0;
  107.    wndClass.cbWndExtra     := 0;
  108.    wndClass.hInstance      := hInstance;
  109.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  110.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  111.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  112.    wndClass.lpszMenuName   := nil;
  113.    wndClass.lpszClassName  := 'GettingStarted';
  114.  
  115.    RegisterClass(wndClass);
  116.  
  117.    hWnd := CreateWindow(
  118.       'GettingStarted',       // window class name
  119.       'Specifying Points on the Boundary',       // window caption
  120.       WS_OVERLAPPEDWINDOW,    // window style
  121.       Integer(CW_USEDEFAULT), // initial x position
  122.       Integer(CW_USEDEFAULT), // initial y position
  123.       Integer(CW_USEDEFAULT), // initial x size
  124.       Integer(CW_USEDEFAULT), // initial y size
  125.       0,                      // parent window handle
  126.       0,                      // window menu handle
  127.       hInstance,              // program instance handle
  128.       nil);                   // creation parameters
  129.  
  130.    ShowWindow(hWnd, SW_SHOW);
  131.    UpdateWindow(hWnd);
  132.  
  133.    while(GetMessage(msg, 0, 0, 0)) do
  134.    begin
  135.       TranslateMessage(msg);
  136.       DispatchMessage(msg);
  137.    end;
  138. end.
  139.